﻿function Change-Csfg 
{
<#
.SYNOPSIS
Change the csconfig file configuration setting, connection string and certificate thumbprint values.
.DESCRIPTION
Change the configuration setting, connection string or certificate thumprint values in a csconfig file.
.EXAMPLE
Change-Cscfg  -File c:\temp\mycscfg.cscfg -Config "MyProperty" -Value "newValue"
.EXAMPLE
Change-Cscfg  -File c:\temp\mycscfg.cscfg -Certificate "MyProperty" -Value "newValue"
.PARAMETER File
The full path to the cscfg file.
.PARAMETER Config
The name of the configuration setting. Exclusive, if this is present, Certificate or ConnectionString cannot be used.
.PARAMETER Certificate
The name of the certificate for the thumbprint. Exclusive, if this is present, Config or ConnectionString cannot be used.
.PARAMETER ConnString
The name of the connection string in the configuration. Exclusive, if this is present, Config or Certificate cannot be used.
.PARAMETER UseSsl
Boolean to set to use ssl or not
.PARAMETER AccountName
Storage account name
.PARAMETER AccountKey
Storage account key
.PARAMETER Value
The new value of the provided configuration setting or thumbprint
#>
[CmdletBinding()]
Param
(
    [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
    [string]$fileName,

    [Parameter(Mandatory=$False)]
    [string]$config,

    [Parameter(Mandatory=$False)]
    [string]$certificate,

    [Parameter(Mandatory=$False)]
    [string]$value,

    [Parameter(Mandatory=$False)]
    [string]$connString,

    [Parameter(Mandatory=$False)]
    [bool]$UseSsl = $true,

    [Parameter(Mandatory=$False)]
    [string]$AccountName,

    [Parameter(Mandatory=$False)]
    [string]$AccountKey
 )
 begin
 {
    
    $serviceConfigNameSpace = @{sc = "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"}

    $cscfg = [xml] (Get-Content $fileName)

    if ($config -ne "" -and $certificate -eq "" -and $connString -eq "" -and $AccountName -eq "" -and $AccountKey -eq "")
    {
        $queryString = "//sc:Setting[@name = '" + $config + "']"
        $configElement = ($cscfg | Select-Xml  $queryString -Namespace $serviceConfigNameSpace)
        if ($configElement -eq $null)
        {
            Write-Host "setting cannot be found."
            return
        }
        $configElement.Node.SetAttribute("value", $value)
        $cscfg.Save($fileName)
        return
    }

    if ($config -eq "" -and $certificate -ne "" -and $connString -eq ""  -and $AccountName -eq "" -and $AccountKey -eq "")
    {
        $queryString = "//sc:Certificate[@name = '" + $certificate + "']"
        $configElement = ($cscfg | Select-Xml  $queryString  -Namespace $serviceConfigNameSpace)
        if ($configElement -eq $null)
        {
            Write-Host "setting cannot be found."
            return
        }
        $configElement.Node.SetAttribute("thumbprint", $value)
        $cscfg.Save($fileName)
        return
    }

    if ($config -eq "" -and $certificate -eq "" -and $connString -ne "" -and $AccountName -ne "" -and $AccountKey -ne "")
    {
        $queryString = "//sc:Setting[@name = '" + $connString + "']"
        $configElement = ($cscfg | Select-Xml  $queryString -Namespace $serviceConfigNameSpace)
        if ($configElement -eq $null)
        {
            Write-Host "setting cannot be found."
            return
        }
        $newConnStr = "DefaultEndpointsProtocol="
        if ($UseSsl)
        {
            $newConnStr = $newConnStr + "https;"
        }
        else
        {
            $newConnStr = $newConnStr + "http;"
        }
        $newConnStr = $newConnStr + "AccountName=" + $AccountName + ";"
        $newConnStr = $newConnStr + "AccountKey=" + $AccountKey 


        $configElement.Node.SetAttribute("value", $newConnStr)
        $cscfg.Save($fileName)
        return
    }


    Write-Error "Invalid usage, see help."
 }

 }



